package com.wyp168.leetcode;

/**
 * @ClassName HammingDistance
 * @Description TODO
 * @Author wyp168
 * @Date 2022/6/26 12:29
 */
public class HammingDistance {
    public static void main(String[] args) {
        System.out.println(hammingDistance(1, 2));
    }

    /**
     * @description: 计算汉明距离（位异或）
     * @author wyp168
     * @date: 2022/6/26 12:30
     */
    public static int hammingDistance(int x, int y) {
        int distance = 0;
        int xoy = x ^ y;
        while (xoy != 0) {
            if ((xoy & 1) == 1) {
                distance ++;
            }
            xoy = xoy >>> 1;
        }
        return distance;
    }
}
